package com.wyix.controller;

import com.wyix.common.PageResult;
import com.wyix.common.Result;
import com.wyix.domain.dto.StudentDTO;
import com.wyix.domain.dto.StudentQueryDTO;
import com.wyix.domain.entity.StudentDO;
import com.wyix.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器
 */
@RestController
@RequestMapping("/api/students")
@Tag(name = "学生管理", description = "学生信息的增删改查接口")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 新增学生
     */
    @PostMapping
    @Operation(summary = "新增学生", description = "添加新的学生信息")
    public Result<?> addStudent(@Valid @RequestBody StudentDTO studentDTO) {
        studentService.addStudent(studentDTO);
        return Result.success();
    }

    /**
     * 根据ID删除学生
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除学生", description = "根据ID删除学生信息")
    public Result<?> deleteStudent(
            @Parameter(description = "学生ID", required = true) @PathVariable Long id) {
        studentService.deleteStudent(id);
        return Result.success();
    }

    /**
     * 更新学生信息
     */
    @PutMapping
    @Operation(summary = "更新学生", description = "更新学生信息")
    public Result<?> updateStudent(@Valid @RequestBody StudentDTO studentDTO) {
        studentService.updateStudent(studentDTO);
        return Result.success();
    }

    /**
     * 根据ID查询学生
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询学生", description = "根据ID查询学生详情")
    public Result<StudentDO> getStudentById(
            @Parameter(description = "学生ID", required = true) @PathVariable Long id) {
        StudentDO student = studentService.getStudentById(id);
        return Result.success(student);
    }

    /**
     * 多条件分页查询学生
     */
    @GetMapping
    @Operation(summary = "查询学生列表", description = "多条件分页查询学生列表")
    public Result<PageResult<StudentDO>> queryStudents(StudentQueryDTO queryDTO) {
        PageResult<StudentDO> pageResult = studentService.queryStudents(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询所有学生
     */
    @GetMapping("/all")
    @Operation(summary = "查询所有学生", description = "查询所有学生信息（不分页）")
    public Result<List<StudentDO>> getAllStudents() {
        List<StudentDO> students = studentService.getAllStudents();
        return Result.success(students);
    }
}
